const Router = require('koa-router')
const mongoose = require('mongoose')

let router = new Router()
router.get('/', async(ctx) => {
  ctx.body = '这是用户操作首页'
})

// 用户登录
router.post('/register', async(ctx) => {
  const User = mongoose.model('User')
  let newUser = new User(ctx.request.body)

  await newUser.save()
    .then(() => {
      ctx.body = {
        code: 200,
        msg: '注册成功'
      }
    }).catch(error => {
      ctx.body = {
        code: 500,
        msg: error
      }
    })
})

// 用户登录
router.post('/login', async (ctx) => {
  let loginUser = ctx.request.body
  console.log(loginUser)
  let { userName } = loginUser
  let { password } = loginUser

  // 引入User的model
  const User = mongoose.model('User')

  await User.findOne({ userName: userName }).exec().then(async result => {
    console.log(result)
    if(result) {
      let newUser = new User()
      await newUser.comparePassword(password, result.password)
        .then(isMatch => {
          if(isMatch) {
            ctx.body = {
              code: 200,
              msg: isMatch
            }
          } else {
            ctx.body = {
              code: 500,
              msg: '密码错误！'
            }
          }
        })
    } else {
      ctx.body = {
        code: 500,
        msg: '用户名不存在'
      }
    }
  }).catch(error => {
    console.log(error)
    ctx.body = {
      code: 500,
      msg: error
    }
  })
})

// 获取用户列表
router.get('/getUserList', async (ctx) => {

  // 引入User的model
  const User = mongoose.model('User')

  await User.find().exec().then(async result => {
    ctx.body = {
      code: 200,
      msg: '请求成功！',
      data: result
    }
  }).catch(error => {
    ctx.body = {
      code: 500,
      msg: error
    }
  })
})

// 获取用户详情
router.get('/getUserInfo', async (ctx) => {
  let { id }  = ctx.request.query
  // 引入User的model
  const User = mongoose.model('User')

  await User.findOne({ _id: id }).exec().then(async result => {
    if(result) {
      ctx.body = {
        code: 200,
        msg: '请求成功！',
        data: result
      }
    } else {
      ctx.body = {
        code: 500,
        msg: '未找到！'
      }
    }
  }).catch(error => {
    console.log(error)
    ctx.body = {
      code: 500,
      msg: error
    }
  })
})

// 修改用户
router.post('/updateUser', async (ctx) => {
  let reqUser = ctx.request.body
  console.log(reqUser)
  let { _id } = reqUser
  // 引入User的model
  const User = mongoose.model('User')

  await User.updateOne({ _id }, {$set: reqUser} ).then(async result => {
    if(result) {
      ctx.body = {
        code: 200,
        msg: '请求成功！',
        data: result
      }
    } else {
      ctx.body = {
        code: 500,
        msg: '未找到！'
      }
    }
  }).catch(error => {
    console.log(error)
    ctx.body = {
      code: 500,
      msg: error
    }
  })
})

// 删除用户
router.get('/deleteUser', async (ctx) => {
  let { id }  = ctx.request.query
  const User = mongoose.model('User')
  await User.deleteOne({ _id: id }).then(async result => {
    ctx.body = {
      code: 200,
      msg: '删除成功！',
      data: result
    }
  }).catch(error => {
    ctx.body = {
      code: 500,
      msg: error
    }
  })
})

module.exports = router
